/*function getTypeName(name) {
    if(name==0){
        return "个人";
    }else if(name==1){
        return "公司";
    }else if(name==2){
        return "管理员"
    }
}*/
window.onload = function(){
    var index = parent.layer.getFrameIndex(window.name);
    var url = window.location.search;//你的地址

    if (url.indexOf("?") != -1) {
        var str = url.substr(1)
        strs = str.split("&");
        var page = strs[0].split("=")[1];
    }
    console.log(page);
    $.ajax({
        url:constUrl+"UserServlet",
        type:'post',
        data:{
            operateType:"showDetail",
            userId:page
        },
        dataType:'json',
        success: function(responseText) {
            console.log(responseText.datas);
            //console.log(9999);
            $("#name").val(responseText.datas[0].userName);
            $("#email").val(responseText.datas[0].userEmail);

            //插入机构数据
            $.ajax({
                url:constUrl+"CompanyServlet",
                type:'post',
                data:{
                    operateType:"showAll",
                    onePageCount:"10",
                    currentPage:"1"
                },
                dataType:'json',
                success: function(responseText1) {
                    var companyHtml ='';
                    for(var i=0;i<responseText1.datas.length;i++){
                        if (responseText.datas[0].companyId == responseText1.datas[i].companyId)
                            companyHtml+='<option value="'+(responseText1.datas[i].companyId)+'" selected="selected">'+responseText1.datas[i].companyName+'</option>'
                        else
                            companyHtml+='<option value="'+(responseText1.datas[i].companyId)+'" >'+responseText1.datas[i].companyName+'</option>'
                    }
                    $("#company_name").append(companyHtml);

                },
                complete: function() {
                },
                error: function() {
                }
            });
            //插入角色数据
            $.ajax({
                url:constUrl+"RoleServlet",
                type:'post',
                data:{
                    operateType:"showAll"
                },
                dataType:'json',
                success: function(responseText2) {
                    var role ='';
                    for(var i=0;i<responseText2.result.roles.length;i++){
                        if (responseText.datas[0].userRole == responseText2.result.roles[i].id)
                            role+='<option value="'+(responseText2.result.roles[i].id)+'" selected="selected">'+responseText2.result.roles[i].name+'</option>'
                        else
                            role+='<option value="'+(responseText2.result.roles[i].id)+'" >'+responseText2.result.roles[i].name+'</option>'
                    }
                    $("#classification").append(role);
                },
                complete: function() {
                },
                error: function() {
                }
            });
        },
        complete: function() {
        },
        error: function() {
        }
    });

    $('#updataUser').click(function () {
        var name = $("#name").val();
        var password = $("#password").val();
        var affiliation = $("#company_name").val();
        var classification = $("#classification").val();
        var email = $("#email").val();

        if(name==""||password==""||classification==""){
            parent.layer.confirm('关键信息未填，不能修改！',{btn:['确定']});
        }else {
            $.ajax({
                url: constUrl + "UserServlet",
                type: 'post',
                data: {
                    operateType: "alter",
                    userName: name,
                    userPassword: password,
                    userAffiliation: affiliation,
                    userRole: classification,
                    userEmail: email,
                    userId:page
                },
                dataType: 'json',
                success: function (dataText) {
                    console.log(dataText.result);
                    if (dataText.result) {
                        parent.layer.confirm('修改成功！', {
                            btn: ['确定']
                        }, function () {
                            parent.location.reload(index);
                            parent.layer.close(index);

                        })

                    }
                },
                complete: function () {
                },
                error: function () {

                }
            });
        }
    });
    $('#closeIframe').click(function(){
        parent.layer.close(index);
    });
}